#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"qrect.h"
#include"qpainter.h"
#include"qtimer.h"
#include"QKeyEvent"
#include<QDebug>
#include"qstring.h"
#include<ctime>
#include "choose.h"
#include "QFont"
#include"beginning.h"

const int TIMER_TIMEOUT=300;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setGeometry(QRect(200,100,800,550));
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event)//程序第一次启动时执行，做一些初始化的工作
{
    QPainter painter(this);
    if(!blsRun)
    {
        InitSlime();
    }
    //画背景
    painter.drawPixmap(background[0],QPixmap(":/images/picture/background.png"));
    painter.drawPixmap(background[1],QPixmap(":/images/picture/background.png"));
    //画史莱姆
    painter.drawPixmap(vSlime,QPixmap(":/images/picture/wind_slime.png"));
    //画子弹
    for(int i=0;i<num_b;i++)
    {
        painter.drawPixmap(bullets[i].bul,QPixmap(":/images/picture/bullet_wind.png"));
    }
    //画墙
    for(int i=0;i<4;i++)
    {
        painter.drawPixmap(walls[i].wall,QPixmap(":/images/picture/walls_3.png"));
    }
    for(int i=4;i<7;i++)
    {
        painter.drawPixmap(walls[i].wall,QPixmap(":/images/picture/walls_2.png"));
    }
    for(int i=7;i<num_wall;i++)
    {
        painter.drawPixmap(walls[i].wall,QPixmap(":/images/picture/walls_3.png"));
    }
    //画源石虫
    for(int i=0;i<num_ena;i++)
    {
        painter.drawPixmap(worm[i].a,QPixmap(":/images/picture/enemy.png"));
    }
    //画属性栏
    painter.drawPixmap(func,QPixmap(":/images/picture/shuxing.png"));
    //画生命值
    for(int i=0;i<3;i++)
    {
        painter.drawPixmap(HP[i].love,QPixmap(":/images/picture/heart.png"));
    }
    //画boss
    painter.drawPixmap(boss_body,QPixmap(":/images/picture/boss.png"));
    painter.setPen(Qt::black);
    painter.setBrush(Qt::red);
    painter.drawRect(boss_blood);
    //画buff
    painter.drawPixmap(addhp,QPixmap(":/images/picture/heart.png"));
    //得分显示
    QFont font2("宋体",25);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(550,530,score_words);
    painter.drawText(680,532,QString::number(score));
    //P1显示
    QFont font3("宋体",25);
    painter.setFont(font3);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(30,535,P1);

    //画有关P2的一切
    painter.drawPixmap(Slime_2P,QPixmap(":/images/picture/2P.png"));
    for(int i=0;i<3;i++)
    {
        painter.drawPixmap(P2_HP[i].love,QPixmap(":/images/picture/heart.png"));
    }//画生命值
    QFont font4("宋体",25);
    painter.setFont(font4);
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawText(450,535,P2);//P2显示
    for(int i=0;i<num_b;i++)
    {
        painter.drawPixmap(P2_bullets[i].bul,QPixmap(":/images/picture/bullet_mushroom.png"));
    }

    //画结束界面
    painter.drawPixmap(ending,QPixmap(":/images/picture/ending.png"));
    painter.drawPixmap(home,QPixmap(":/images/picture/home.png"));
    painter.drawPixmap(try_again,QPixmap(":/images/picture/try_again.png"));
    painter.drawPixmap(next,QPixmap(":/images/picture/next.png"));
    //显示游戏开始与结束
    QFont font1("字魂103号-海棠手书",65);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(230,270,display);
}
void MainWindow::InitSlime()
{
    QRect rect(100,200,30,20);//史莱姆的初始位置
    vSlime=rect;//史莱姆的矩形身体
    Slime_2P.setRect(140,200,30,30);
    func.setRect(0,500,800,50);//属性栏的初始化
    boss_body.setRect(801,200,100,100);//boss的初始化
    HP[0].love.setRect(80,505,40,40);
    HP[1].love.setRect(130,505,40,40);
    HP[2].love.setRect(180,505,40,40);//画生命
    P2_HP[0].love.setRect(300,505,40,40);
    P2_HP[1].love.setRect(350,505,40,40);
    P2_HP[2].love.setRect(400,505,40,40);//P2画生命

    background[0].setRect(0,0,800,500);
    background[1].setRect(799,0,800,500);//画背景
    blsRun=true;
    score=0;
    score_words="score:";
    P1="P1";
    P2="P2";
    this->wallmove();
    this->enemyattack();

    timer=new QTimer(this);//设定史莱姆计时器
    timer->start(0);
    connect(timer,SIGNAL(timeout()),SLOT(Slime_update()));

    btimer=new QTimer(this);//设定子弹计时器
    btimer->start(10);
    connect(btimer,SIGNAL(timeout()),SLOT(bullet_update()));

    wtimer=new QTimer(this);//设定墙的计时器
    wtimer->start(60);
    connect(wtimer,SIGNAL(timeout()),SLOT(wall_update()));

    enatimer=new QTimer(this);//设定源石虫的计时器
    enatimer->start(30);
    connect(enatimer,SIGNAL(timeout()),SLOT(enemy_update()));

    bosstimer=new QTimer(this);//设定boss的计时器
    bosstimer->start(speed);
    connect(bosstimer,SIGNAL(timeout()),SLOT(boss_update()));

    backtimer=new QTimer(this);//设定背景计时器
    backtimer->start(1);
    connect(backtimer,SIGNAL(timeout()),SLOT(background_update()));

    keytimer=new QTimer(this);//设定按键的计时器
    keytimer->start(TIMER_TIMEOUT);
    keytimer->stop();
}
void MainWindow::keyPressEven(QEvent *e)
{
     QKeyEvent *keyEvent=static_cast<QKeyEvent *>(e);
     switch(keyEvent->key()){
     case  Qt::Key_W:{
          pressKey.k_w=1;//设置按键w为按下状态
          break;
     }
     case  Qt::Key_A:
            {
          pressKey.k_a=1;//设置按键a为按下状态
          break;
     }
     case  Qt::Key_S:
     {
          pressKey.k_s=1;//设置按键s为按下状态
          break;
     }
     case  Qt::Key_D:
     {
          pressKey.k_d=1;//设置按键d为按下状态
          break;
     }
     case  Qt::Key_J:
     {
          pressKey.k_j=1;//设置按键j为按下状态
          break;
     }
     case  Qt::Key_Up:
     {
          pressKey.k_1=1;//设置按键上为按下状态
          break;
     }
     case  Qt::Key_Left:
     {
          pressKey.k_2=1;//设置按键左为按下状态
          break;
     }
     case  Qt::Key_Down:
     {
          pressKey.k_3=1;//设置按键下为按下状态
          break;
     }
     case  Qt::Key_Right:
     {
          pressKey.k_4=1;//设置按键a为按下状态
          break;
     }
     case  Qt::Key_Space:
     {
          pressKey.k_5=1;//设置按键a为按下状态
          break;
     }
    }
    if(pressKey.haveKeyDown()&&!keytimer->isActive())//如果计时器处于停止状态且有一个键已经被按下(总数>1)则开启计时器
    {
        keytimer->start(TIMER_TIMEOUT);
    }
    return QWidget::keyPressEvent(keyEvent);
}
void MainWindow::keyReleaseEven(QEvent *e)
{
    QKeyEvent *keyEvent=static_cast<QKeyEvent *>(e);
    switch(keyEvent->key()){
    case  Qt::Key_W:
    {
          pressKey.k_w=0;
          break;
    }
    case  Qt::Key_A:
    {
          pressKey.k_a=0;
          break;
    }
    case  Qt::Key_S:
    {
          pressKey.k_s=0;
          break;
    }
    case  Qt::Key_D:
    {
          pressKey.k_d=0;
          break;
    }
    case  Qt::Key_J:
    {
          pressKey.k_j=0;
          break;
    }
    case  Qt::Key_Up:
    {
          pressKey.k_1=0;
          break;
    }
    case  Qt::Key_Left:
    {
          pressKey.k_2=0;
          break;
    }
    case  Qt::Key_Down:
    {
          pressKey.k_3=0;
          break;
    }
    case  Qt::Key_Right:
    {
          pressKey.k_4=0;
          break;
    }
    case  Qt::Key_Space:
    {
          pressKey.k_5=0;
          break;
    }
    }//恢复按键的状态
     if(keytimer->isActive()&&!pressKey.haveKeyDown())
     {
               keytimer->stop(); // 如果没有键按下停止计时器。
     }
     e->accept();
    return QWidget::keyReleaseEvent(keyEvent);
}
bool MainWindow::event(QEvent *e)
{
    if(e->type()==QEvent::KeyPress)
    {
        keyPressEven(e);//键盘按下事件
    }
    if(e->type()==QEvent::KeyRelease)
    {
        keyReleaseEven(e);//键盘抬起事件
        return true;//阻止事件被分发
    }
    return QMainWindow::event(e);//将键盘事件分发下去
}
void MainWindow::Slime_update()
{
    if(alive)
    {
    if( pressKey.k_w==1&&vSlime.top()>0)
     {
        vSlime.setTop(vSlime.top()-2);
        vSlime.setBottom(vSlime.bottom()-2);
     }
     if(pressKey.k_a==1&&vSlime.left()>0)
     {
         vSlime.setLeft(vSlime.left()-2);
         vSlime.setRight(vSlime.right()-2);
     }
     if(pressKey.k_s==1&&vSlime.bottom()<499)
     {
         vSlime.setTop(vSlime.top()+2);
         vSlime.setBottom(vSlime.bottom()+2);
     }
     if(pressKey.k_d==1&&vSlime.right()<799)
     {
         vSlime.setLeft(vSlime.left()+2);
         vSlime.setRight(vSlime.right()+2);
     }
     if(pressKey.k_j==1)
     {
         this->shoot();
     }
    }

    if(P2_alive)
    {
     if( pressKey.k_1==1&&Slime_2P.top()>0)
     {
         Slime_2P.setTop(Slime_2P.top()-2);
         Slime_2P.setBottom(Slime_2P.bottom()-2);
     }
     if(pressKey.k_2==1&&Slime_2P.left()>0)
     {
         Slime_2P.setLeft(Slime_2P.left()-2);
         Slime_2P.setRight(Slime_2P.right()-2);
     }
     if(pressKey.k_3==1&&Slime_2P.bottom()<499)
     {
         Slime_2P.setTop(Slime_2P.top()+2);
         Slime_2P.setBottom(Slime_2P.bottom()+2);
     }
     if(pressKey.k_4==1&&Slime_2P.right()<799)
     {
         Slime_2P.setLeft(Slime_2P.left()+2);
         Slime_2P.setRight(Slime_2P.right()+2);
     }
     if(pressKey.k_5==1)
     {
         this->P2_shoot();
     }
    }

    display="";
    if(!alive&&!P2_alive){blsStop=true;}
    if(blsStop)
    {
        timer->stop();
        btimer->stop();
        wtimer->stop();
        enatimer->stop();
        bosstimer->stop();
        backtimer->stop();
        ending.setRect(200,150,400,200);
        if(boss_arrive&&boss_blood.right()<=boss_blood.left())
        {
            display=" You win! ";
            home.setRect(250,370,50,50);
            try_again.setRect(380,370,50,50);
            next.setRect(510,370,50,50);
        }
        else
        {
            display="Game Over!";
            home.setRect(300,370,50,50);
            try_again.setRect(430,370,50,50);
        }
    }
    update();//paintEvent更新
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    QPoint dot=e->pos();
    if(display=="Game Over!"&&e->button()==Qt::LeftButton&&dot.x()>=300&&dot.x()<=350&&dot.y()>=370&&dot.y()<=420)
    {
        this->close();
        beginning *beg=new beginning();
        beg->show();
    }
    if(display=="Game Over!"&&e->button()==Qt::LeftButton&&dot.x()>=430&&dot.x()<=480&&dot.y()>=370&&dot.y()<=420)
    {
        this->close();
        MainWindow *main=new MainWindow();
        main->show();
    }
    if(display==" You win! "&&e->button()==Qt::LeftButton&&dot.x()>=250&&dot.x()<=300&&dot.y()>=370&&dot.y()<=420)
    {
        this->close();
        beginning *beg=new beginning();
        beg->show();
    }
    if(display==" You win! "&&e->button()==Qt::LeftButton&&dot.x()>=380&&dot.x()<=430&&dot.y()>=370&&dot.y()<=420)
    {
        this->close();
        MainWindow *main=new MainWindow();
        main->show();
    }
}
void MainWindow::shoot()//子弹的生成位置
{
    if(bullets[0].bullet_use)
    {
        bullets[0].bul.setTop(vSlime.top()+5);
        bullets[0].bul.setBottom(vSlime.bottom()-5);
        bullets[0].bul.setLeft(vSlime.right());
        bullets[0].bul.setRight(vSlime.right()+10);
        bullets[0].bullet_use=false;
    }
    for(int i=1;i<num_b;i++)
    {
        if(bullets[i].bullet_use&&bullets[i-1].bul.left()>=vSlime.right()+700)
        {
            bullets[i].bul.setTop(vSlime.top()+5);
            bullets[i].bul.setBottom(vSlime.bottom()-5);
            bullets[i].bul.setLeft(vSlime.right());
            bullets[i].bul.setRight(vSlime.right()+20);
            bullets[i].bullet_use=false;
        }
    }
}
void MainWindow::P2_shoot()
{
    if(P2_bullets[0].bullet_use)
        {
            P2_bullets[0].bul.setTop(Slime_2P.top()+10);
            P2_bullets[0].bul.setBottom(Slime_2P.bottom()-10);
            P2_bullets[0].bul.setLeft(Slime_2P.right());
            P2_bullets[0].bul.setRight(Slime_2P.right()+10);
            P2_bullets[0].bullet_use=false;
        }
        for(int i=1;i<num_b;i++)
        {
            if(P2_bullets[i].bullet_use&&P2_bullets[i-1].bul.left()>=Slime_2P.right()+700)
            {
                P2_bullets[i].bul.setTop(Slime_2P.top()+10);
                P2_bullets[i].bul.setBottom(Slime_2P.bottom()-10);
                P2_bullets[i].bul.setLeft(Slime_2P.right());
                P2_bullets[i].bul.setRight(Slime_2P.right()+20);
                P2_bullets[i].bullet_use=false;
            }
        }
}
void MainWindow::bullet_update()//实现子弹的移动
{
    for(int i=0;i<num_b;i++)
    {
        if(!bullets[i].bullet_use)
        {
            bullets[i].bul.setLeft(bullets[i].bul.left()+10);
            bullets[i].bul.setRight(bullets[i].bul.right()+10);
        }

        //使子弹不能穿过墙
        if(bullets[i].bul.right()>=walls[0].wall.left()&&vSlime.left()<=walls[0].wall.right()&&(bullets[i].bul.bottom()<=walls[0].wall.bottom()+10||bullets[i].bul.top()>=walls[1].wall.top()-10))
        {bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
        if(bullets[i].bul.right()>=walls[2].wall.left()&&vSlime.left()<=walls[2].wall.right()&&(bullets[i].bul.bottom()<=110||bullets[i].bul.top()>=190))
        {bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
        if(bullets[i].bul.right()>=walls[4].wall.left()&&vSlime.left()<=walls[4].wall.right()&&((bullets[i].bul.bottom()<=148&&bullets[i].bul.top()>=78)||(bullets[i].bul.bottom()<=287&&bullets[i].bul.top()>=215)||(bullets[i].bul.bottom()<=422&&bullets[i].bul.top()>=352)))
        {bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
        for(int j=7;j<26;j+=2)
        {
            if(bullets[i].bul.right()>=walls[j].wall.left()&&vSlime.left()<=walls[j].wall.right()&&(bullets[i].bul.bottom()<=walls[j].wall.bottom()+10||bullets[i].bul.top()>=walls[j+1].wall.top()-10))
            {bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
        }
        for(int j=27;j<35;j++)
        {
            if(bullets[i].bul.right()>=walls[j].wall.left()&&vSlime.left()<=walls[j].wall.right()&&bullets[i].bul.bottom()<=walls[j].wall.bottom()+10&&bullets[i].bul.top()>=walls[j].wall.top()-10)
            {bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
        }

        //子弹与敌人相遇后都会消失
        for(int j=0;j<num_ena;j++)
        {
            if(worm[j].a.right()<=800&&bullets[i].bul.left()<=worm[j].a.right()&&bullets[i].bul.right()>=worm[j].a.left()&&bullets[i].bul.top()>=worm[j].a.top()-10&&bullets[i].bul.bottom()<=worm[j].a.bottom()+10)
            {
                bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;
                worm[j].a.setRect(0,0,0,0);j=100;
                score+=20;
            }
        }
        //子弹与boss碰撞后子弹消失，boss掉血,速度加快
        if(boss_arrive&&bullets[i].bul.right()>=boss_body.left()&&bullets[i].bul.left()<=boss_body.right()&&bullets[i].bul.top()>=boss_body.top()-10&&bullets[i].bul.bottom()<=boss_body.bottom()+10)
        {
            bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;
            boss_blood.setRight(boss_blood.right()-10);
            speed-=2;change_body++;
        }
        if(boss_arrive&&boss_blood.right()<=boss_blood.left()){score+=500;blsStop=true;}
        //如果子弹没有射中boss，boss会加血
        if(bullets[i].bul.left()>=800&&boss_arrive)
        {
            bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;
            if(boss_blood.right()<700){
            boss_blood.setRight(boss_blood.right()+10);
            speed+=2;change_body--;}
        }
        if(bullets[i].bul.left()>=800){bullets[i].bul.setRect(0,0,0,0);bullets[i].bullet_use=true;}
    }

    //P2的子弹
    for(int i=0;i<num_b;i++)
    {
        if(!P2_bullets[i].bullet_use)
        {
            P2_bullets[i].bul.setLeft(P2_bullets[i].bul.left()+10);
            P2_bullets[i].bul.setRight(P2_bullets[i].bul.right()+10);
        }

        //使子弹不能穿过墙
        if(P2_bullets[i].bul.right()>=walls[0].wall.left()&&Slime_2P.left()<=walls[0].wall.right()&&(P2_bullets[i].bul.bottom()<=walls[0].wall.bottom()+10||P2_bullets[i].bul.top()>=walls[1].wall.top()-10))
        {P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
        if(P2_bullets[i].bul.right()>=walls[2].wall.left()&&Slime_2P.left()<=walls[2].wall.right()&&(P2_bullets[i].bul.bottom()<=110||P2_bullets[i].bul.top()>=190))
        {P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
        if(P2_bullets[i].bul.right()>=walls[4].wall.left()&&Slime_2P.left()<=walls[4].wall.right()&&((P2_bullets[i].bul.bottom()<=148&&P2_bullets[i].bul.top()>=78)||(P2_bullets[i].bul.bottom()<=287&&P2_bullets[i].bul.top()>=215)||(P2_bullets[i].bul.bottom()<=422&&P2_bullets[i].bul.top()>=352)))
        {P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
        for(int j=7;j<26;j+=2)
        {
            if(P2_bullets[i].bul.right()>=walls[j].wall.left()&&Slime_2P.left()<=walls[j].wall.right()&&(P2_bullets[i].bul.bottom()<=walls[j].wall.bottom()+10||P2_bullets[i].bul.top()>=walls[j+1].wall.top()-10))
            {P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
        }
        for(int j=27;j<35;j++)
        {
            if(P2_bullets[i].bul.right()>=walls[j].wall.left()&&Slime_2P.left()<=walls[j].wall.right()&&P2_bullets[i].bul.bottom()<=walls[j].wall.bottom()+10&&P2_bullets[i].bul.top()>=walls[j].wall.top()-10)
            {P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
        }

        //子弹与源石虫相遇后都会消失
        for(int j=0;j<num_ena;j++)
        {
            if(worm[j].a.right()<=800&&P2_bullets[i].bul.left()<=worm[j].a.right()&&P2_bullets[i].bul.right()>=worm[j].a.left()&&P2_bullets[i].bul.top()>=worm[j].a.top()-10&&P2_bullets[i].bul.bottom()<=worm[j].a.bottom()+10)
            {
                P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;
                worm[j].a.setRect(0,0,0,0);j=100;
                score+=20;
            }
        }
        //子弹与boss碰撞后子弹消失，boss掉血,速度加快
        if(boss_arrive&&P2_bullets[i].bul.right()>=boss_body.left()&&P2_bullets[i].bul.left()<=boss_body.right()&&P2_bullets[i].bul.top()>=boss_body.top()-10&&P2_bullets[i].bul.bottom()<=boss_body.bottom()+10)
        {
            P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;
            boss_blood.setRight(boss_blood.right()-10);
            speed-=2;change_body++;
        }
        if(boss_arrive&&boss_blood.right()<=boss_blood.left()){score+=500;blsStop=true;}
        //如果子弹没有射中boss，boss会加血
        if(P2_bullets[i].bul.left()>=800&&boss_arrive)
        {
            P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;
            if(boss_blood.right()<700){
            boss_blood.setRight(boss_blood.right()+10);
            speed+=2;change_body--;}
        }
        if(P2_bullets[i].bul.left()>=800){P2_bullets[i].bul.setRect(0,0,0,0);P2_bullets[i].bullet_use=true;}
    }
    update();
}
void MainWindow::wallmove()//墙的生成位置
{
    walls[0].wall.setTop(0);walls[0].wall.setBottom(200);walls[0].wall.setLeft(801);walls[0].wall.setRight(851);
    walls[1].wall.setTop(300);walls[1].wall.setBottom(500);walls[1].wall.setLeft(801);walls[1].wall.setRight(851);

    walls[2].wall.setTop(0);walls[2].wall.setBottom(100);walls[2].wall.setLeft(801);walls[2].wall.setRight(851);
    walls[3].wall.setTop(200);walls[3].wall.setBottom(500);walls[3].wall.setLeft(801);walls[3].wall.setRight(851);

    walls[4].wall.setTop(88);walls[4].wall.setBottom(138);walls[4].wall.setLeft(801);walls[4].wall.setRight(1201);
    walls[5].wall.setTop(225);walls[5].wall.setBottom(275);walls[5].wall.setLeft(801);walls[5].wall.setRight(1201);
    walls[6].wall.setTop(362);walls[6].wall.setBottom(412);walls[6].wall.setLeft(801);walls[6].wall.setRight(1201);

    walls[7].wall.setTop(0);walls[7].wall.setBottom(50);walls[7].wall.setLeft(801);walls[7].wall.setRight(901);
    walls[8].wall.setTop(200);walls[8].wall.setBottom(500);walls[8].wall.setLeft(801);walls[8].wall.setRight(901);
    walls[9].wall.setTop(0);walls[9].wall.setBottom(100);walls[9].wall.setLeft(901);walls[9].wall.setRight(1001);
    walls[10].wall.setTop(250);walls[10].wall.setBottom(500);walls[10].wall.setLeft(901);walls[10].wall.setRight(1001);
    walls[11].wall.setTop(0);walls[11].wall.setBottom(150);walls[11].wall.setLeft(1001);walls[11].wall.setRight(1101);
    walls[12].wall.setTop(300);walls[12].wall.setBottom(500);walls[12].wall.setLeft(1001);walls[12].wall.setRight(1101);
    walls[13].wall.setTop(0);walls[13].wall.setBottom(200);walls[13].wall.setLeft(1101);walls[13].wall.setRight(1201);
    walls[14].wall.setTop(350);walls[14].wall.setBottom(500);walls[14].wall.setLeft(1101);walls[14].wall.setRight(1201);
    walls[15].wall.setTop(0);walls[15].wall.setBottom(250);walls[15].wall.setLeft(1201);walls[15].wall.setRight(1301);
    walls[16].wall.setTop(400);walls[16].wall.setBottom(500);walls[16].wall.setLeft(1201);walls[16].wall.setRight(1301);
    addhp.setTop(370);addhp.setBottom(400);addhp.setLeft(1286);addhp.setRight(1316);

    walls[17].wall.setTop(0);walls[17].wall.setBottom(250);walls[17].wall.setLeft(1301);walls[17].wall.setRight(1401);
    walls[18].wall.setTop(400);walls[18].wall.setBottom(500);walls[18].wall.setLeft(1301);walls[18].wall.setRight(1401);
    walls[19].wall.setTop(0);walls[19].wall.setBottom(200);walls[19].wall.setLeft(1401);walls[19].wall.setRight(1501);
    walls[20].wall.setTop(350);walls[20].wall.setBottom(500);walls[20].wall.setLeft(1401);walls[20].wall.setRight(1501);
    walls[21].wall.setTop(0);walls[21].wall.setBottom(150);walls[21].wall.setLeft(1501);walls[21].wall.setRight(1601);
    walls[22].wall.setTop(300);walls[22].wall.setBottom(500);walls[22].wall.setLeft(1501);walls[22].wall.setRight(1601);
    walls[23].wall.setTop(0);walls[23].wall.setBottom(100);walls[23].wall.setLeft(1601);walls[23].wall.setRight(1701);
    walls[24].wall.setTop(250);walls[24].wall.setBottom(500);walls[24].wall.setLeft(1601);walls[24].wall.setRight(1701);
    walls[25].wall.setTop(0);walls[25].wall.setBottom(50);walls[25].wall.setLeft(1701);walls[25].wall.setRight(1801);
    walls[26].wall.setTop(200);walls[26].wall.setBottom(500);walls[26].wall.setLeft(1701);walls[26].wall.setRight(1801);

    walls[27].wall.setTop(200);walls[27].wall.setBottom(300);walls[27].wall.setLeft(801);walls[27].wall.setRight(901);
    walls[28].wall.setTop(150);walls[28].wall.setBottom(350);walls[28].wall.setLeft(901);walls[28].wall.setRight(1001);
    walls[29].wall.setTop(100);walls[29].wall.setBottom(400);walls[29].wall.setLeft(1001);walls[29].wall.setRight(1101);
    walls[30].wall.setTop(50);walls[30].wall.setBottom(450);walls[30].wall.setLeft(1101);walls[30].wall.setRight(1201);
    walls[31].wall.setTop(50);walls[31].wall.setBottom(450);walls[31].wall.setLeft(1201);walls[31].wall.setRight(1301);
    walls[32].wall.setTop(100);walls[32].wall.setBottom(400);walls[32].wall.setLeft(1301);walls[32].wall.setRight(1401);
    walls[33].wall.setTop(150);walls[33].wall.setBottom(350);walls[33].wall.setLeft(1401);walls[33].wall.setRight(1501);
    walls[34].wall.setTop(200);walls[34].wall.setBottom(300);walls[34].wall.setLeft(1501);walls[34].wall.setRight(1601);
}
void MainWindow::wall_update()//实现墙的移动
{

        walls[0].wall.setLeft(walls[0].wall.left()-10);walls[0].wall.setRight(walls[0].wall.right()-10);
        walls[1].wall.setLeft(walls[1].wall.left()-10);walls[1].wall.setRight(walls[1].wall.right()-10);
        if(walls[0].wall.right()<=400)
        {
            walls[2].wall.setLeft(walls[2].wall.left()-10);walls[2].wall.setRight(walls[2].wall.right()-10);
            walls[3].wall.setLeft(walls[3].wall.left()-10);walls[3].wall.setRight(walls[3].wall.right()-10);
        }
        if(walls[2].wall.right()<=400)
        {
            walls[4].wall.setLeft(walls[4].wall.left()-10);walls[4].wall.setRight(walls[4].wall.right()-10);
            walls[5].wall.setLeft(walls[5].wall.left()-10);walls[5].wall.setRight(walls[5].wall.right()-10);
            walls[6].wall.setLeft(walls[6].wall.left()-10);walls[6].wall.setRight(walls[6].wall.right()-10);
        }
        if(walls[6].wall.right()<=400)
        {
            walls[7].wall.setLeft(walls[7].wall.left()-10);walls[7].wall.setRight(walls[7].wall.right()-10);
            walls[8].wall.setLeft(walls[8].wall.left()-10);walls[8].wall.setRight(walls[8].wall.right()-10);
            walls[9].wall.setLeft(walls[9].wall.left()-10);walls[9].wall.setRight(walls[9].wall.right()-10);
            walls[10].wall.setLeft(walls[10].wall.left()-10);walls[10].wall.setRight(walls[10].wall.right()-10);
            walls[11].wall.setLeft(walls[11].wall.left()-10);walls[11].wall.setRight(walls[11].wall.right()-10);
            walls[12].wall.setLeft(walls[12].wall.left()-10);walls[12].wall.setRight(walls[12].wall.right()-10);
            walls[13].wall.setLeft(walls[13].wall.left()-10);walls[13].wall.setRight(walls[13].wall.right()-10);
            walls[14].wall.setLeft(walls[14].wall.left()-10);walls[14].wall.setRight(walls[14].wall.right()-10);
            walls[15].wall.setLeft(walls[15].wall.left()-10);walls[15].wall.setRight(walls[15].wall.right()-10);
            walls[16].wall.setLeft(walls[16].wall.left()-10);walls[16].wall.setRight(walls[16].wall.right()-10);
            if(!boss_arrive){addhp.setLeft(addhp.left()-10);addhp.setRight(addhp.right()-10);}
            walls[17].wall.setLeft(walls[17].wall.left()-10);walls[17].wall.setRight(walls[17].wall.right()-10);
            walls[18].wall.setLeft(walls[18].wall.left()-10);walls[18].wall.setRight(walls[18].wall.right()-10);
            walls[19].wall.setLeft(walls[19].wall.left()-10);walls[19].wall.setRight(walls[19].wall.right()-10);
            walls[20].wall.setLeft(walls[20].wall.left()-10);walls[20].wall.setRight(walls[20].wall.right()-10);
            walls[21].wall.setLeft(walls[21].wall.left()-10);walls[21].wall.setRight(walls[21].wall.right()-10);
            walls[22].wall.setLeft(walls[22].wall.left()-10);walls[22].wall.setRight(walls[22].wall.right()-10);
            walls[23].wall.setLeft(walls[23].wall.left()-10);walls[23].wall.setRight(walls[23].wall.right()-10);
            walls[24].wall.setLeft(walls[24].wall.left()-10);walls[24].wall.setRight(walls[24].wall.right()-10);
            walls[25].wall.setLeft(walls[25].wall.left()-10);walls[25].wall.setRight(walls[25].wall.right()-10);
            walls[26].wall.setLeft(walls[26].wall.left()-10);walls[26].wall.setRight(walls[26].wall.right()-10);
        }
        if(walls[26].wall.right()<=400)
        {
            walls[27].wall.setLeft(walls[27].wall.left()-10);walls[27].wall.setRight(walls[27].wall.right()-10);
            walls[28].wall.setLeft(walls[28].wall.left()-10);walls[28].wall.setRight(walls[28].wall.right()-10);
            walls[29].wall.setLeft(walls[29].wall.left()-10);walls[29].wall.setRight(walls[29].wall.right()-10);
            walls[30].wall.setLeft(walls[30].wall.left()-10);walls[30].wall.setRight(walls[30].wall.right()-10);
            walls[31].wall.setLeft(walls[31].wall.left()-10);walls[31].wall.setRight(walls[31].wall.right()-10);
            walls[32].wall.setLeft(walls[32].wall.left()-10);walls[32].wall.setRight(walls[32].wall.right()-10);
            walls[33].wall.setLeft(walls[33].wall.left()-10);walls[33].wall.setRight(walls[33].wall.right()-10);
            walls[34].wall.setLeft(walls[34].wall.left()-10);walls[34].wall.setRight(walls[34].wall.right()-10);
        }

        //使史莱姆不会穿过墙
        if(vSlime.right()>=walls[0].wall.left()&&vSlime.left()<=walls[0].wall.right()&&(vSlime.top()<=walls[0].wall.bottom()||vSlime.bottom()>=walls[1].wall.top()))
        {vSlime.setRight(walls[0].wall.left());vSlime.setLeft(vSlime.right()-30);}
        if(vSlime.right()>=walls[2].wall.left()&&vSlime.left()<=walls[2].wall.right()&&(vSlime.top()<=walls[2].wall.bottom()||vSlime.bottom()>=walls[3].wall.top()))
        {vSlime.setRight(walls[2].wall.left());vSlime.setLeft(vSlime.right()-30);}
        if(vSlime.right()>=walls[4].wall.left()&&vSlime.left()<=walls[4].wall.right()&&((vSlime.top()<=138&&vSlime.top()>=68)||(vSlime.top()<=275&&vSlime.top()>=205)||(vSlime.top()<=412&&vSlime.top()>=322)))
        {vSlime.setRight(walls[4].wall.left());vSlime.setLeft(vSlime.right()-30);}
        for(int i=7;i<=25;i+=2)
        {
             if(vSlime.right()>walls[i].wall.left()&&vSlime.left()<walls[i].wall.right()&&(vSlime.top()<walls[i].wall.bottom()||vSlime.bottom()>walls[i+1].wall.top()))
             {
                 vSlime.setRight(walls[i].wall.left());vSlime.setLeft(vSlime.right()-30);
             }

        }
        for(int i=27;i<num_wall;i++)
        {
             if(vSlime.right()>=walls[i].wall.left()&&vSlime.left()<=walls[i].wall.right()&&vSlime.top()>=walls[i].wall.top()-20&&vSlime.top()<=walls[i].wall.bottom())
             {
                 vSlime.setRight(walls[i].wall.left());vSlime.setLeft(vSlime.right()-30);
             }

        }

        //使P2不会穿过墙
        if(Slime_2P.right()>=walls[0].wall.left()&&Slime_2P.left()<=walls[0].wall.right()&&(Slime_2P.top()<=walls[0].wall.bottom()||Slime_2P.bottom()>=walls[1].wall.top()))
        {Slime_2P.setRight(walls[0].wall.left());Slime_2P.setLeft(Slime_2P.right()-30);}
        if(Slime_2P.right()>=walls[2].wall.left()&&Slime_2P.left()<=walls[2].wall.right()&&(Slime_2P.top()<=walls[2].wall.bottom()||Slime_2P.bottom()>=walls[3].wall.top()))
        {Slime_2P.setRight(walls[2].wall.left());Slime_2P.setLeft(Slime_2P.right()-30);}
        if(Slime_2P.right()>=walls[4].wall.left()&&Slime_2P.left()<=walls[4].wall.right()&&((Slime_2P.top()<=138&&Slime_2P.top()>=58)||(Slime_2P.top()<=275&&Slime_2P.top()>=195)||(Slime_2P.top()<=412&&Slime_2P.top()>=332)))
        {Slime_2P.setRight(walls[4].wall.left());Slime_2P.setLeft(Slime_2P.right()-30);}
        for(int i=7;i<=25;i+=2)
        {
             if(Slime_2P.right()>walls[i].wall.left()&&Slime_2P.left()<walls[i].wall.right()&&(Slime_2P.top()<walls[i].wall.bottom()||Slime_2P.bottom()>walls[i+1].wall.top()))
             {
                 Slime_2P.setRight(walls[i].wall.left());Slime_2P.setLeft(Slime_2P.right()-30);
             }

        }
        for(int i=27;i<num_wall;i++)
        {
             if(Slime_2P.right()>=walls[i].wall.left()&&Slime_2P.left()<=walls[i].wall.right()&&Slime_2P.top()>=walls[i].wall.top()-20&&Slime_2P.top()<=walls[i].wall.bottom())
             {
                 Slime_2P.setRight(walls[i].wall.left());Slime_2P.setLeft(Slime_2P.right()-30);
             }

        }

        //boss血量低于50%时，召唤墙壁
        if(walls[0].wall_boss)
        {
            if(!walls[0].wall_use)
            {
                int n=rand()%400;
                walls[0].wall.setBottom(n);walls[0].wall.setLeft(801);walls[0].wall.setRight(851);
                walls[1].wall.setTop(walls[0].wall.bottom()+100);walls[1].wall.setLeft(801);walls[1].wall.setRight(851);
                walls[0].wall_use=true;
            }
            if(walls[0].wall_use)
            {
                walls[0].wall.setLeft(walls[0].wall.left()-10); walls[1].wall.setLeft(walls[1].wall.left()-10);
                walls[0].wall.setRight(walls[0].wall.right()-10); walls[1].wall.setRight(walls[1].wall.right()-10);
                if(walls[0].wall.right()<=-200){walls[0].wall_use=false;}
            }
        }
        //史莱姆被墙推到左边，史莱姆生命值归零
        if(vSlime.right()<=0)
        {
            alive=false;
            for(int i=0;i<3;i++)
            {
                HP[i].love.setRect(0,0,0,0);HP[i].hp_use=false;
            }
        }
        if(Slime_2P.right()<=0)
        {
            P2_alive=false;
            for(int i=0;i<3;i++)
            {
                P2_HP[i].love.setRect(0,0,0,0);P2_HP[i].hp_use=false;
            }
        }
        //史莱姆吃到buff，补充生命
        if(vSlime.right()>=addhp.left()&&vSlime.left()<=addhp.right()&&vSlime.top()>=addhp.top()-20&&vSlime.top()<=addhp.bottom())
        {
            this->addheart();addhp.setRect(0,0,0,0);
            if(boss_arrive){addhp_use=false;}
        }
        //P2补充生命
        if(Slime_2P.right()>=addhp.left()&&Slime_2P.left()<=addhp.right()&&Slime_2P.top()>=addhp.top()-20&&Slime_2P.top()<=addhp.bottom())
        {
            this->P2_addheart();addhp.setRect(0,0,0,0);
            if(boss_arrive){addhp_use=false;}
        }
        //boss战来临时，每隔一段时间会有回血buff
        if(boss_arrive)
        {
            if(!addhp_use)
            {
                int m=rand()%270+100;
                addhp.setTop(m);addhp.setBottom(m+30);addhp.setLeft(801);addhp.setRight(831);
                addhp_use=true;
            }
            if(addhp_use)
            {
                if(addhp.right()>=100)
                {
                    addhp.setLeft(addhp.left()-2);addhp.setRight(addhp.right()-2);
                }
            }
        }

    update();
}
void MainWindow::enemyattack()//敌人的生成位置
{
    worm[0].a.setRect(801,242,16,16);worm[1].a.setRect(801,142,16,16);worm[2].a.setRect(801,36,16,16);
    worm[3].a.setRect(801,174,16,16);worm[4].a.setRect(801,310,16,16);worm[5].a.setRect(801,464,16,16);
    worm[6].a.setRect(801,17,16,16);worm[7].a.setRect(801,466,16,16);worm[8].a.setRect(801,242,16,16);
    worm[9].a.setRect(867,177,16,16);worm[10].a.setRect(867,242,16,16);worm[11].a.setRect(867,307,16,16);
    worm[12].a.setRect(933,112,16,16);worm[13].a.setRect(933,242,16,16);worm[14].a.setRect(933,372,16,16);
    worm[15].a.setRect(999,47,16,16);worm[16].a.setRect(999,242,16,16);worm[17].a.setRect(999,437,16,16);
    worm[18].a.setRect(1065,242,16,16);worm[19].a.setRect(1131,242,16,16);worm[20].a.setRect(1197,242,16,16);
    worm[21].a.setRect(1262,242,16,16);
}
void MainWindow::enemy_update()//实现敌人的移动
{
    if(walls[0].wall.right()<=500){worm[0].a.setLeft(worm[0].a.left()-10);worm[0].a.setRight(worm[0].a.right()-10);}
    if(walls[2].wall.right()<=500){worm[1].a.setLeft(worm[1].a.left()-10);worm[1].a.setRight(worm[1].a.right()-10);}
    if(walls[4].wall.left()<=500)
    {
        worm[2].a.setLeft(worm[2].a.left()-10);worm[2].a.setRight(worm[2].a.right()-10);
        worm[3].a.setLeft(worm[3].a.left()-10);worm[3].a.setRight(worm[3].a.right()-10);
        worm[4].a.setLeft(worm[4].a.left()-10);worm[4].a.setRight(worm[4].a.right()-10);
        worm[5].a.setLeft(worm[5].a.left()-10);worm[5].a.setRight(worm[5].a.right()-10);
    }
    if(walls[31].wall.right()<=500)
    {
        worm[6].a.setLeft(worm[6].a.left()-10);worm[6].a.setRight(worm[6].a.right()-10);
        worm[7].a.setLeft(worm[7].a.left()-10);worm[7].a.setRight(worm[7].a.right()-10);
    }
    if(walls[34].wall.right()<=0)
    {
        for(int i=8;i<num_ena;i++){worm[i].a.setLeft(worm[i].a.left()-10);worm[i].a.setRight(worm[i].a.right()-10);}
    }
    //史莱姆碰到源石虫，扣一滴血
    for(int i=0;i<num_ena;i++)
    {
        if(vSlime.right()>=worm[i].a.left()&&vSlime.left()<=worm[i].a.right()&&vSlime.top()>=worm[i].a.top()-20&&vSlime.bottom()<=worm[i].a.bottom()+20)
        {this->lostheart();worm[i].a.setRect(0,0,0,0);i=100;}
    }
    //P2碰到源石虫，扣一滴血
    for(int i=0;i<num_ena;i++)
    {
        if(Slime_2P.right()>=worm[i].a.left()&&Slime_2P.left()<=worm[i].a.right()&&Slime_2P.top()>=worm[i].a.top()-20&&Slime_2P.bottom()<=worm[i].a.bottom()+20)
        {this->P2_lostheart();worm[i].a.setRect(0,0,0,0);i=100;}
    }

    update();
}
void MainWindow::lostheart()//损失生命
{
    for(int i=2;i>=0;i--)
    {
        if(HP[i].hp_use){HP[i].love.setRect(0,0,0,0);HP[i].hp_use=false;break;}
    }
    if(!HP[0].hp_use){alive=false;vSlime.setRect(0,0,0,0);}
}
void MainWindow::P2_lostheart()//P2损失生命
{
    for(int i=2;i>=0;i--)
    {
        if(P2_HP[i].hp_use){P2_HP[i].love.setRect(0,0,0,0);P2_HP[i].hp_use=false;break;}
    }
    if(!P2_HP[0].hp_use){P2_alive=false;Slime_2P.setRect(0,0,0,0);}
}
void MainWindow::addheart()
{
    for(int i=1;i<3;i++)
    {
        if(!HP[i].hp_use){HP[i].love.setRect(HP[i-1].love.left()+50,505,40,40);HP[i].hp_use=true;break;}
    }
}
void MainWindow::P2_addheart()
{
    for(int i=1;i<3;i++)
    {
        if(!P2_HP[i].hp_use){P2_HP[i].love.setRect(P2_HP[i-1].love.left()+50,505,40,40);P2_HP[i].hp_use=true;break;}
    }
}
void MainWindow::boss_update()//实现boss的移动
{
    //boss速度的更新
    if(boss_arrive){bosstimer->stop();bosstimer->start(speed);}
    //boss血量小于一半，召唤墙壁
    if(boss_blood.right()<=400&&!walls[0].wall_boss&&boss_arrive){walls[0].wall_boss=true;}
    //boss何时生成
    if(walls[34].wall.right()<=-1000&&boss_body.left()>=550)
    {boss_body.setLeft(boss_body.left()-10);boss_body.setRight(boss_body.right()-10);}
    if(walls[34].wall.right()<=-1600&&!boss_arrive){boss_blood.setRect(100,470,600,10);boss_arrive=true;worm[0].enemy_use=true;}
    //boss到达指定位置开始上下移动
    if(boss_blood.left()==100&&boss_up){boss_body.setTop(boss_body.top()-10);boss_body.setBottom(boss_body.bottom()-10);}
    if(boss_blood.left()==100&&!boss_up){boss_body.setTop(boss_body.top()+10);boss_body.setBottom(boss_body.bottom()+10);}
    //boss体型的变化
    if(boss_arrive){boss_body.setRight(641-change_body);boss_body.setBottom(boss_body.top()+100-change_body);}
    //boss的上下移动
    if(boss_body.top()<=0){boss_up=false;}if(boss_body.bottom()>=500){boss_up=true;}
    //boss也会发射子弹
    if(worm[0].enemy_use)
    {
        worm[0].a.setLeft(worm[0].a.left()-10);
        worm[0].a.setRight(worm[0].a.right()-10);
        if(worm[0].a.right()<=0)
        {
            worm[0].enemy_use=false;
            worm[1].a.setTop((boss_body.top()+boss_body.bottom())/2-8);
            worm[1].a.setLeft(boss_body.left()-17);
            worm[1].a.setBottom(worm[0].a.top()+16);
            worm[1].a.setRight(worm[0].a.left()+16);
            worm[1].enemy_use=true;
        }
    }
    if(worm[1].enemy_use)
    {
        worm[1].a.setLeft(worm[0].a.left()-10);
        worm[1].a.setRight(worm[0].a.right()-10);
        if(worm[1].a.right()<=0)
        {
            worm[1].enemy_use=false;
            worm[0].a.setTop((boss_body.top()+boss_body.bottom())/2+8);
            worm[0].a.setLeft(boss_body.left()-17);
            worm[0].a.setBottom(worm[0].a.top()+16);
            worm[0].a.setRight(worm[0].a.left()+16);
            worm[0].enemy_use=true;
        }
    }

    update();
}
void MainWindow::background_update()
{
    background[0].setLeft(background[0].left()-1);background[0].setRight(background[0].right()-1);
    background[1].setLeft(background[1].left()-1);background[1].setRight(background[1].right()-1);
    if(background[0].right()<=0){background[0].setLeft(799);background[0].setRight(background[0].left()+800);}
    if(background[1].right()<=0){background[1].setLeft(799);background[1].setRight(background[1].left()+800);}
    update();
}
